/*
 * pwm.c
 *
 *  Created on: 05/09/2013
 *      Author: Facu
 */

#include "LPC17xx.h"
#include "type.h"
#include "pwm.h"
/*Conexion LED RGB

GPIO2 P2.5
	  P2.4
	  P2.3

*/

void rgb_init(void){

	LPC_SC->PCONP |= (1<<6); //enable PWM1 Power

	//DEFAULT CLOCK 100Mhz/4 = 25mhz

	//Configuro los pines como PWM
	LPC_PINCON->PINSEL4 |= (1 << 6);
	LPC_PINCON->PINSEL4 |= (1 << 8);
	LPC_PINCON->PINSEL4 |= (1 << 10);

	//FRECUENCIA DE PWM - 1KHZ

	LPC_PWM1->PR = 98;  //count frequency:Fpclk
	//LPC_PWM1->PR = 10;
	LPC_PWM1->MCR = 1 << 1;			/*	reset on MR0 */
	//LPC_PWM1->MR0 = (1<<PWM_BITS) - 2;		/* PWM CYCLE */
	LPC_PWM1->MR0 = 255;


	//LEDs default to OFF
	LPC_PWM1->MR4 = 0; //Azul
	LPC_PWM1->MR5 = 0; //Rojo
    LPC_PWM1->MR6 = 0; //VERDE

    LPC_PWM1->LER = LER4_EN | LER5_EN | LER6_EN ; //Load Shadow register contents
    LPC_PWM1->PCR = PWMENA4 | PWMENA5 | PWMENA6 ; 			//Enable PWM outputs
    LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN;				//Enable PWM Timer

    /*
     * MR0 = 255
MR1 = rojo (salida PWM1)
MR2 = verde (salida PWM2)
MR3 = azul (salida PWM3)

     */


}


void rgb_set(uint8_t a, char color){

	if(color == 'R'){

	LPC_PWM1->MR5 = a;
	LPC_PWM1->LER = LER5_EN; //Load Shadow register contents
	}else if(color == 'G'){
	LPC_PWM1->MR6 = a;
	LPC_PWM1->LER = LER6_EN; //Load Shadow register contents
	}
	else if(color =='B'){
	LPC_PWM1->MR4 = a;
	LPC_PWM1->LER = LER4_EN; //Load Shadow register contents
	}
}

void rgb_setAll(uint8_t a){

	LPC_PWM1->MR4 = a;

	LPC_PWM1->MR5 = a;
	LPC_PWM1->MR6 = a;

	LPC_PWM1->LER =  LER4_EN |LER5_EN | LER6_EN; //Load Shadow register contents

}




